%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is part of the set of files that accompany the article:       %
%Mankiw, N. Gregory and Ricardo Reis (2007) "Sticky Information in  %
%General Equilibrium," Journal of the European Economic Association,%
%forthcoming. See the appendix of the NBER or CEPR working paper    %
%versions for a detailed explanation of the algorithms.             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Please cite if you use the programs. I do not provide tech support.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Last revised: August 30, 2006                                      %
%Written by: Ricardo Reis                                           %
%Input: File with MLE parameters MLEresults.mat                     %
%Output: Variance decompositions MLEvd.mat                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%% STEP1: Variance decomposition for the MLE estimates %%%%
clear; clc;
load MLEresults.mat;
x=pars_c; theta=MLE;
x(4)=theta(1); x(5)=theta(2); x(12)=theta(3); x(13)=theta(4);
x(14)=theta(5); x(15)=theta(6); x(16)=theta(7); x(17)=theta(8);
x(18)=theta(9); x(19)=theta(10); x(20)=theta(11);
VD_mle=SIGEvardec(x);

%%%% STEP2: Variance decomposition for many (Num) draws %%%%
VARDEC=[]; Num=1000;
h = waitbar(0,'Going over variance decompositions...');
for i=1:Num
    theta=mvnrnd(MLE,VARCOV);
    x(4)=theta(1); x(5)=theta(2); x(12)=theta(3); x(13)=theta(4);
    x(14)=theta(5); x(15)=theta(6); x(16)=theta(7); x(17)=theta(8);
    x(18)=theta(9); x(19)=theta(10); x(20)=theta(11);
    VARDEC=[VARDEC SIGEvardec(x)];
    waitbar(i/Num,h);
end
close(h);
for i=1:5
    for j=1:5
        for k=1:Num
            z(i,j,k)=VARDEC(i,j+5*(k-1));
        end
    VD_025(i,j)=prctile(z(i,j,:),2.5);
    VD_500(i,j)=prctile(z(i,j,:),50);
    VD_975(i,j)=prctile(z(i,j,:),97.5);
    end
end

clear MLE Num SE VARCOV VARDEC fval hess i j k pars_c theta x h
save MLEvd.mat